//
//  HouseInfoGrabRootDelegate.m
//  Jyxt
//
//  Created by 刘日 on 17/5/17.
//  Copyright © 2017年 DaLian Rental Housing Management. All rights reserved.
//

#import "HouseInfoGrabRootDelegate.h"
#import "PhotosTableViewCell.h"
#import "HouseInfoGrabViewModel.h"
#import "AddOwnerViewController.h"
#import <YYKit.h>

@interface HouseInfoGrabRootDelegate()

@end

@implementation HouseInfoGrabRootDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.section == 0 ? [PhotosTableViewCell calculateHeight] : (indexPath.section == 2 && indexPath.row < [HouseInfoGrabViewModel owners].count ? 60 : 48);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2 && indexPath.row < [HouseInfoGrabViewModel owners].count) {
        // 编辑共有人
        AddOwnerViewController *add = (AddOwnerViewController *)[LKInstance findViewControllerInStoryboardWithKey: @"m_addOwner"];
        add.editIndex = @(indexPath.row);
        [LKFreeCtrl pushViewController: add];
    }
}

- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSMutableArray<UITableViewRowAction *> *actions = [NSMutableArray array];
    if (indexPath.section == 2 && indexPath.row < [HouseInfoGrabViewModel owners].count) {
        [actions addObject: [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title: @"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
            [[HouseInfoGrabViewModel owners] removeObjectAtIndex: indexPath.row];
            [tableView reloadSection: 2 withRowAnimation: UITableViewRowAnimationAutomatic];
        }]];
    }
    return actions;
}

@end
